package com.cdl.library.dialog;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cdl.library.R;
import com.cdl.library.config.DialogConfig;

public class ProgressBarDialog {
    private Context context;
    private Dialog dialog;
    private TextView dialog_title;
    ProgressBar progress_circular;
    boolean onKeyBack = true;

    /***
     * 是否点击返回按钮消失
     * @param context
     * @param onKeyBack
     * true 弹窗消失
     * false 不消失
     */
    public ProgressBarDialog(Activity context, boolean onKeyBack) {
        this.context = context;
        this.onKeyBack = onKeyBack;
        dialog = new Dialog(context, R.style.MyDialog_Base);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View dialog_view = View.inflate(context, R.layout.dialog_progressbar, null);
        dialog.setContentView(dialog_view);
        WindowManager.LayoutParams params = context.getWindow().getAttributes();
        params.width = DialogConfig.screenWidth / 2;
        params.height = ActionBar.LayoutParams.WRAP_CONTENT;
        dialog.setCancelable(false); // true 点击屏幕以外关闭dialog
        dialog_title = (TextView) dialog_view.findViewById(R.id.dialog_title);
        progress_circular = dialog_view.findViewById(R.id.progress_circular);
        initListener();
    }

    private void initListener() {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && onKeyBack) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
    }

    int delayTimeDelay = 0;

    public void show(String title, int progress, int delayTime) {
        delayTimeDelay = delayTime;
        dialog_title.setText(title);
        progress_circular.setProgress(progress);
        dialog.show();
        startTimeDelay();
    }


    private Handler handler = new Handler();
    private void startTimeDelay() {
        if (delayTimeDelay < 1000) {
            return;
        }
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, delayTimeDelay);
    }

    public void updateShowProgress(int valuesProgress) {
        startTimeDelay();
        progress_circular.setProgress(valuesProgress);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            dismiss();
        }
    };

    /**
     * 关闭对话框
     */
    public void dismiss() {
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否显示
     *
     * @return 是否显示
     */
    public boolean isShow() {
        if (dialog == null) {
            return false;
        }
        return dialog.isShowing();
    }

    public void setValuesMax(int maxValues) {
        progress_circular.setMax(maxValues);
    }


}
